import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.Scanner;

/**
 * @program: java_zzx
 * @description:
 * @author: zzx
 * @create: 2022-11-09 15:20
 **/
public class textDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入文件名：");
        String filename = sc.nextLine();
        if (filename.trim().isEmpty()) {
            System.out.println("请正确输入文件名！");
            return;
        }
        Path path = Paths.get(filename);
        if (Files.exists(path)) {
            if (Files.isDirectory(path)) {
                System.out.println(path + " 是一个目录!");
            } else {
                System.out.println(filename + "文件已存在！");
            }
        }else {
            Path parent = path.getParent();
            if (parent != null && !Files.exists(parent)) {
                try {
                    Files.createDirectories(parent);
                    System.out.println("父目录已创建：" + parent);
                } catch (IOException e) {
                    System.out.println("无法创建父目录："+ parent);
                }
            }else {
                try (PrintStream printStream = new PrintStream(filename,"GBK")) {
                    printStream.println("Hello");
                    printStream.println("当前时间：" + LocalDateTime.now().toString());
                }catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
    }
}
